package com.alibaba.algo.apiimpl.user;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercomponent.service.user.SysEmployeeOrgService;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectEmployeeOrgByOrgIdRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectEmployeeRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectEmployeeOrgByOrgIdResponseVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectUserDetailResponseVO;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.domain.user.SysEmployeeOrgDomain;
import com.alibaba.algo.entity.user.SysEmployeeOrg;
import com.alibaba.algo.exception.BaseErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类SysEmployeeOrgServiceImpl.java的实现描述：员工和组织关联表 serviceimpl
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Slf4j
@RestController
public class SysEmployeeOrgServiceImpl implements SysEmployeeOrgService {

    @Autowired
    private SysEmployeeOrgDomain sysEmployeeOrgDomain;

    @Override
    public void insert(Long employeeId, Long orgId) {
        SysEmployeeOrg sysEmployeeOrg = new SysEmployeeOrg();
        sysEmployeeOrg.setEmployeeId(employeeId);
        sysEmployeeOrg.setOrgId(orgId);
        sysEmployeeOrgDomain.insert(sysEmployeeOrg);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        //sysEmployeeOrgMapper.deleteByEmployeeId(employeeId);
    }

    /**
     * 查询组织下的员工列表
     *
     * @param vo 组织编码
     * @return 员工列表
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult selectListByOrgId(SelectEmployeeOrgByOrgIdRequestVO vo) {
        if (ObjectUtils.isEmpty(vo.getOrgId())) {
            throw new BaseException(BaseErrorCode.ORG_ID_IS_NULL.getCode(), BaseErrorCode.ORG_ID_IS_NULL.getMessage());
        }
        // 查询组织下的员工列表
        List<SelectEmployeeOrgByOrgIdResponseVO> resList = sysEmployeeOrgDomain.selectEmployeeListByOrgId(vo.getOrgId());
        return BaseResult.buildSuccess(resList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = Exception.class)
    public BaseResult selectEmployeeListInOrgByName(SelectEmployeeRequestVO vo) {
        // 查询员工列表(组织下存在的)
        Map<Object,Boolean> map = new HashMap<>();
        List<SelectUserDetailResponseVO> resList = sysEmployeeOrgDomain.selectEmployeeListInOrgByName(vo).stream().filter(i -> map.putIfAbsent(i.getId(), Boolean.TRUE) == null).collect(Collectors.toList());
        return BaseResult.buildSuccess(resList);
    }
}
